package com.ruipengkj.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.ruipengkj.bean.TWorkShift;
import com.ruipengkj.commons.dao.GenericDao;
import com.ruipengkj.dao.TWorkShiftDao;

@Repository
public class TWorkShiftDaoImpl extends GenericDao<TWorkShift,String> implements TWorkShiftDao{
	
	public TWorkShift findSingleEntity(final String hql,final String whereHQL,final List<Object> param) throws RuntimeException {
		
		return (TWorkShift) getHiberanteTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
//				Session session = getHiberanteTemplate().getSessionFactory().openSession();
				
				Query query = session.createQuery(hql + buildWhereHQL(whereHQL)) ;
				setQueryParams(query, param) ;
				
				return (TWorkShift) query.uniqueResult() ;
			}
		}) ;
	}
	
	public TWorkShift findLastOne(final String hql){
		Session session = getHiberanteTemplate().getSessionFactory().openSession();
		
		Query query = session.createQuery(hql) ;
		TWorkShift tws = (TWorkShift) query.list().get(0);
		return tws;
	}
	
	public TWorkShift findLastOneByEmployee(final String hql,List<Object> param){
		Session session = getHiberanteTemplate().getSessionFactory().openSession();
		
		Query query = session.createQuery(hql) ;
		
		if(param.size()!=0){
			for(int i = 0;i < param.size();i++){
//				query.setParameter(i, param.get(i));
				query.setString(i, param.get(i).toString());
			}
		}
		TWorkShift tws=(TWorkShift) query.uniqueResult(); 
		if(tws!=null){
			return tws;
		}
		return null;
	}

	
}
